/**
 * 您可以将常用的方法、或系统 API，统一封装，暴露全局，以便各页面、组件调用，而无需 require / import.
 */
const prompt = require('@system.prompt')

/**
 * 拼接 url 和参数
 */
function queryString(url, query) {
  let str = []
  for (let key in query) {
    str.push(key + '=' + query[key])
  }
  let paramStr = str.join('&')
  return paramStr ? `${url}?${paramStr}` : url
}

function showToast(message = '', duration = 0) {
  if (!message) return
  prompt.showToast({
    message: message,
    duration
  })
}

function formatTimestamp(timestamp) {
  const date = new Date(timestamp);

  // 获取月日格式
  const month = date.getMonth() + 1; // 月份从0开始
  const day = date.getDate();
  const monthDay = `${month}月${day}日`;

  // 获取星期数
  const weekDays = ['日', '一', '二', '三', '四', '五', '六'];
  const weekDay = weekDays[date.getDay()];

  // 获取时分格式
  let hours = date.getHours();
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const ampm = hours >= 12 ? 'PM' : 'AM';

  // 转换为12小时制
  hours = hours % 12;
  hours = hours ? String(hours).padStart(2, '0') : '12'; // 如果小时为0，则设置为12

  const time = `${hours}:${minutes} ${ampm}`;

  return {
    monthDay,
    weekDay,
    time,
  };
}

function getWeekday(dateString) {
  // 将字符串转换为日期对象
  const year = parseInt(dateString.slice(0, 4), 10);
  const month = parseInt(dateString.slice(4, 6), 10) - 1; // 月份从0开始
  const day = parseInt(dateString.slice(6, 8), 10);

  const date = new Date(year, month, day);

  // 获取星期数
  const weekdays = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
  const weekdayIndex = date.getDay(); // 获取星期数，0-6表示星期天到星期六

  return weekdays[weekdayIndex];
}

function getWeatherDescription(code) {
  const weatherData = {
    0: "晴天",
    1: "多云",
    2: "阴",
    3: "阵雨",
    4: "雷阵雨",
    5: "雷阵雨伴有冰雹",
    6: "雨夹雪",
    7: "小雨",
    8: "中雨",
    9: "大雨",
    10: "暴雨",
    11: "大暴雨",
    12: "特大暴雨",
    13: "阵雪",
    14: "小雪",
    15: "中雪",
    16: "大雪",
    17: "暴雪",
    18: "雾",
    19: "冻雨",
    20: "沙尘暴",
    21: "小到中雨",
    22: "中到大雨",
    23: "大到暴雨",
    24: "暴雨到大暴雨",
    25: "大暴雨到特大暴雨",
    26: "小到中雪",
    27: "中到大雪",
    28: "大到暴雪",
    29: "浮尘",
    30: "扬沙",
    31: "强沙尘暴",
    53: "霾"
  };

  return weatherData[code] || "未知天气";
}

function getWeatherIconPath(inputString) {

  const weatherIcons = {
    "晴天": "/assets/images/weather-icon/tianqi-icon/sunny.png",
    "多云": "/assets/images/weather-icon/tianqi-icon/cloudy.png",
    "阴": "/assets/images/weather-icon/tianqi-icon/dreary.png",
    "阵雨": "/assets/images/weather-icon/tianqi-icon/showers.png",
    "雷阵雨": "/assets/images/weather-icon/tianqi-icon/thundershower.png",
    "雷阵雨伴有冰雹": "/assets/images/weather-icon/tianqi-icon/thundershower_hail.png",
    "雨夹雪": "/assets/images/weather-icon/tianqi-icon/rain_snow.png",
    "小雨": "/assets/images/weather-icon/tianqi-icon/light_rain.png",
    "中雨": "/assets/images/weather-icon/tianqi-icon/moderate_rain.png",
    "大雨": "/assets/images/weather-icon/tianqi-icon/heavy_rain.png",
    "暴雨": "/assets/images/weather-icon/tianqi-icon/storm.png",
    "大暴雨": "/assets/images/weather-icon/tianqi-icon/heavy_storm.png",
    "特大暴雨": "/assets/images/weather-icon/tianqi-icon/severe_storm.png",
    "阵雪": "/assets/images/weather-icon/tianqi-icon/snow_showers.png",
    "小雪": "/assets/images/weather-icon/tianqi-icon/light_snow.png",
    "中雪": "/assets/images/weather-icon/tianqi-icon/moderate_snow.png",
    "大雪": "/assets/images/weather-icon/tianqi-icon/heavy_snow.png",
    "暴雪": "/assets/images/weather-icon/tianqi-icon/snowstorm.png",
    "雾": "/assets/images/weather-icon/tianqi-icon/fog.png",
    "冻雨": "/assets/images/weather-icon/tianqi-icon/freezing_rain.png",
    "沙尘暴": "/assets/images/weather-icon/tianqi-icon/dust_storm.png",
    "小到中雨": "/assets/images/weather-icon/tianqi-icon/light_moderate_rain.png",
    "中到大雨": "/assets/images/weather-icon/tianqi-icon/moderate_heavy_rain.png",
    "大到暴雨": "/assets/images/weather-icon/tianqi-icon/heavy_storm_rain.png",
    "暴雨到大暴雨": "/assets/images/weather-icon/tianqi-icon/storm_heavy_storm.png",
    "大暴雨到特大暴雨": "/assets/images/weather-icon/tianqi-icon/heavy_severe_storm.png",
    "小到中雪": "/assets/images/weather-icon/tianqi-icon/light_moderate_snow.png",
    "中到大雪": "/assets/images/weather-icon/tianqi-icon/moderate_heavy_snow.png",
    "大到暴雪": "/assets/images/weather-icon/tianqi-icon/heavy_snowstorm.png",
    "浮尘": "/assets/images/weather-icon/tianqi-icon/dust.png",
    "扬沙": "/assets/images/weather-icon/tianqi-icon/sand.png",
    "强沙尘暴": "/assets/images/weather-icon/tianqi-icon/sandstorm.png",
    "霾": "/assets/images/weather-icon/tianqi-icon/haze.png"
  };

  return weatherIcons[inputString] || "/assets/images/weather-icon/tianqi-icon/sunny.png"; // 默认图标


}

function getWeatherWarningIcon(inputString) {

  const warningIcons = {
    "台风": "/assets/images/weather-icon/yujin-icon/typhoon.png",
    "暴雨": "/assets/images/weather-icon/yujin-icon/heavy_rain.png",
    "暴雪": "/assets/images/weather-icon/yujin-icon/heavy_snow.png",
    "寒潮": "/assets/images/weather-icon/yujin-icon/cold_wave.png",
    "大风": "/assets/images/weather-icon/yujin-icon/strong_wind.png",
    "沙尘暴": "/assets/images/weather-icon/yujin-icon/dust_storm.png",
    "高温": "/assets/images/weather-icon/yujin-icon/high_temperature.png",
    "干旱": "/assets/images/weather-icon/yujin-icon/drought.png",
    "雷电": "/assets/images/weather-icon/yujin-icon/lightning.png",
    "冰雹": "/assets/images/weather-icon/yujin-icon/hail.png",
    "霜冻": "/assets/images/weather-icon/yujin-icon/frost.png",
    "大雾": "/assets/images/weather-icon/yujin-icon/fog.png",
    "霾": "/assets/images/weather-icon/yujin-icon/haze.png",
    "道路结冰": "/assets/images/weather-icon/yujin-icon/icy_road.png",
    "其他": "/assets/images/weather-icon/yujin-icon/other.png",
    "雷电大风": "/assets/images/weather-icon/yujin-icon/lightning_strong_wind.png",
    "森林火险": "/assets/images/weather-icon/yujin-icon/forest_fire.png",
    "寒冷": "/assets/images/weather-icon/yujin-icon/cold.png",
    "灰霾": "/assets/images/weather-icon/yujin-icon/gray_haze.png",
    "降温": "/assets/images/weather-icon/yujin-icon/cooling.png",
    "道路冰雪": "/assets/images/weather-icon/yujin-icon/icy_snow_road.png",
    "干热风": "/assets/images/weather-icon/yujin-icon/dry_hot_wind.png",
    "空气重污染": "/assets/images/weather-icon/yujin-icon/heavy_pollution.png",
    "冰冻": "/assets/images/weather-icon/yujin-icon/frozen.png",
    "海上大雾": "/assets/images/weather-icon/yujin-icon/sea_fog.png",
    "雷暴大风": "/assets/images/weather-icon/yujin-icon/thunderstorm_strong_wind.png",
    "持续低温": "/assets/images/weather-icon/yujin-icon/persistent_cold.png",
    "浓浮尘": "/assets/images/weather-icon/yujin-icon/thick_dust.png",
    "龙卷风": "/assets/images/weather-icon/yujin-icon/tornado.png",
    "低温冻害": "/assets/images/weather-icon/yujin-icon/low_temperature_freeze.png",
    "海上大风": "/assets/images/weather-icon/yujin-icon/sea_strong_wind.png",
    "低温雨雪冰冻": "/assets/images/weather-icon/yujin-icon/low_temp_rain_snow_freeze.png",
    "强对流": "/assets/images/weather-icon/yujin-icon/strong_convection.png",
    "臭氧": "/assets/images/weather-icon/yujin-icon/ozone.png",
    "大雪": "/assets/images/weather-icon/yujin-icon/big_snow.png",
    "强降雨": "/assets/images/weather-icon/yujin-icon/strong_rain.png",
    "强降温": "/assets/images/weather-icon/yujin-icon/strong_cooling.png",
    "雪灾": "/assets/images/weather-icon/yujin-icon/snow_disaster.png",
    "森林（草原）火险": "/assets/images/weather-icon/yujin-icon/forest_grassland_fire.png",
    "雷暴": "/assets/images/weather-icon/yujin-icon/thunderstorm.png",
    "严寒": "/assets/images/weather-icon/yujin-icon/severe_cold.png",
    "沙尘": "/assets/images/weather-icon/yujin-icon/dust.png",
    "海上雷雨大风": "/assets/images/weather-icon/yujin-icon/sea_thunderstorm_strong_wind.png",
    "海上雷电": "/assets/images/weather-icon/yujin-icon/sea_lightning.png",
    "海上台风": "/assets/images/weather-icon/yujin-icon/sea_typhoon.png",
    "低温": "/assets/images/weather-icon/yujin-icon/low_temperature.png",
    "地质灾害": "/assets/images/weather-icon/yujin-icon/geological_disaster.png",
    "地质灾害气象风险": "/assets/images/weather-icon/yujin-icon/geological_risk.png",
    "山洪": "/assets/images/weather-icon/yujin-icon/mudslide.png",
    "草原火险": "/assets/images/weather-icon/yujin-icon/prairie_fire.png",
    "雷雨大风": "/assets/images/weather-icon/yujin-icon/thunderstorm_rain_strong_wind.png"
  };

  return warningIcons[inputString] || "/assets/images/weather-icon/yujin-icon/typhoon.png"; // 默认图标
}

function throttle(func, limit = 1000, that) {
  let lastFunc;
  let lastRan;

  return (...args) => {
    const context = that;

    if (!lastRan) {
      func.apply(context, args);
      lastRan = Date.now();
    } else {
      clearTimeout(lastFunc);
      lastFunc = setTimeout(() => {
        if (Date.now() - lastRan >= limit) {
          func.apply(context, args);
          lastRan = Date.now();
        }
      }, limit - (Date.now() - lastRan));
    }
  };
}

function timeAgo(dateString) {
  const now = new Date();
  const past = new Date(dateString);
  const diffInSeconds = Math.floor((now - past) / 1000);
  
  if (diffInSeconds < 60) {
    return `${diffInSeconds}秒前`;
  } else if (diffInSeconds < 3600) {
    return `${Math.floor(diffInSeconds / 60)}分钟前`;
  } else if (diffInSeconds < 86400) {
    return `${Math.floor(diffInSeconds / 3600)}小时前`;
  } else if (diffInSeconds < 2592000) {
    return `${Math.floor(diffInSeconds / 86400)}天前`;
  } else if (diffInSeconds < 31536000) {
    return `${Math.floor(diffInSeconds / 2592000)}个月前`;
  } else {
    return `${Math.floor(diffInSeconds / 31536000)}年前`;
  }
}

function formatTimestampCopy(timestamp) {
  const date = new Date(timestamp); // 创建 Date 对象
  const hours = String(date.getUTCHours()).padStart(2, '0'); // 获取小时并格式化
  const minutes = String(date.getUTCMinutes()).padStart(2, '0'); // 获取分钟并格式化
  const seconds = String(date.getUTCSeconds()).padStart(2, '0'); // 获取秒并格式化
  const milliseconds = String(date.getUTCMilliseconds()).padStart(3, '0'); // 获取毫秒并格式化

  return `${hours}:${minutes}:${seconds}.${milliseconds}`; // 返回格式化字符串
}




export default {
  showToast,
  queryString,
  formatTimestamp,
  getWeekday,
  getWeatherDescription,
  getWeatherIconPath,
  throttle,
  getWeatherWarningIcon,
  timeAgo,
  formatTimestampCopy
}
